package com.hzmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzmall.common.ServerResponse;
import com.hzmall.dao.ShippingMapper;
import com.hzmall.pojo.Shipping;
import com.hzmall.service.IShippingService;
import com.hzmall.vo.ShippingVo;
import org.apache.commons.collections.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("shippingService")
public class ShippingService implements IShippingService {

    @Autowired
    private ShippingMapper shippingMapper;
    /**
     * 增加收货地址
     */
    public ServerResponse add(Integer userId, Shipping shipping){
        shipping.setUserId(userId);
        Integer rows = this.shippingMapper.insertSelective(shipping);
        if (rows > 0){
            return ServerResponse.createBySuccess(shipping.getId(),"新增收货地址成功");
        }
        return ServerResponse.createByErrorMessage("新增收货地址失败");
    }

    /**
     * 删除收货地址
     */
    public ServerResponse del(Integer userId, Integer shippingId){
        Integer rows = this.shippingMapper.deleteShippingByUserIdAndId(userId, shippingId);
        if (rows > 0){
            return ServerResponse.createBySuccessMessage("删除地址成功");
        }
        return ServerResponse.createByErrorMessage("删除失败");
    }

    /**
     * 登录状态更新收货地址
     */
    public ServerResponse update(Integer userId,Shipping shipping){
        Shipping existShipping = this.shippingMapper.selectByPrimaryKey(shipping.getId());
        if (existShipping == null){
            return ServerResponse.createByErrorMessage("该收货地址不存在");
        }
        if (!existShipping.getUserId().equals(userId)){
            return ServerResponse.createByErrorMessage("非法调用");
        }
        Integer rows = this.shippingMapper.updateByPrimaryKeySelective(shipping);
        if (rows > 0){
            return ServerResponse.createBySuccessMessage("更新收货地址成功");
        }
        return ServerResponse.createByErrorMessage("更新收货地址失败");
    }

    /**
     * 查看选中地址
     */
    public ServerResponse select(Integer userId,Integer shippingId){
        Shipping shipping = this.shippingMapper.selectShippingByUseridAndShippingId(userId, shippingId);
        if (shipping == null){
            return ServerResponse.createByErrorMessage("该收货地址不存在");
        }
        ShippingVo shippingVo = convertShipping2ShippingVo(shipping);
        return ServerResponse.createBySuccess(shippingVo);
    }

    public ShippingVo convertShipping2ShippingVo(Shipping shipping){
        ShippingVo shippingVo = new ShippingVo();
        shippingVo.setCreateTime(shipping.getCreateTime());
        shippingVo.setReceiverAddress(shipping.getReceiverAddress());
        shippingVo.setId(shipping.getId());
        shippingVo.setReceiverCity(shipping.getReceiverCity());
        shippingVo.setReceiverMobile(shipping.getReceiverMobile());
        shippingVo.setReceiverName(shipping.getReceiverName());
        shippingVo.setReceiverPhone(shipping.getReceiverPhone());
        shippingVo.setReceiverProvince(shipping.getReceiverProvince());
        shippingVo.setReceiverZip(shipping.getReceiverZip());
        shippingVo.setUpdateTime(shipping.getUpdateTime());
        shippingVo.setUserId(shipping.getUserId());
        return shippingVo;
    }

    /**
     * 收货地址列表
     */
    public ServerResponse list(Integer userId,Integer pageNum,Integer pageSize){
        List<Shipping> shippingList = this.shippingMapper.selectShippingsByUserId(userId);
        List<ShippingVo> shippingVoList = new ArrayList<ShippingVo>();
        PageHelper.startPage(pageNum, pageSize);
        if (shippingList != null){
            for (Shipping shippingItem : shippingList){
                ShippingVo shippingVo = convertShipping2ShippingVo(shippingItem);
                shippingVoList.add(shippingVo);
            }
        }
        PageInfo pageInfo = new PageInfo(shippingList);
        pageInfo.setList(shippingVoList);
        return ServerResponse.createBySuccess(pageInfo);
    }
}
